package com.geilizu.dom.service.impl;

import com.geilizu.dom.dao.BuildingDao;
import com.geilizu.dom.dao.DormitoryDao;
import com.geilizu.dom.dto.AbsentDto;
import com.geilizu.dom.dto.SearchByName;
import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.Absent;
import com.geilizu.dom.dao.AbsentDao;
import com.geilizu.dom.service.AbsentService;
import com.geilizu.dom.service.DormitoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Absent)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 18:27:16
 */
@Service("absentService")
public class AbsentServiceImpl implements AbsentService {
    @Resource
    private AbsentDao absentDao;

    @Override
    public Integer save(Absent absent) {
        return absentDao.save(absent);
    }

    @Override
    public Map<String, Object> list(Integer page, Integer size) {
        page = (page - 1) * size;
        List<AbsentDto> absentList = absentDao.queryAll(page, size);
        Long count = absentDao.queryAllCount();
        Map<String, Object> map = new HashMap<>();
        map.put("absentList", absentList);
        map.put("count", count);
        return map;

    }

    @Override
    public Map<String, Object> search(SearchDto searchDto) {
        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
        List<AbsentDto> absentList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getValue().equals("")) {
            absentList = absentDao.queryAll(page, searchDto.getSize());
            count = absentDao.queryAllCount();
        } else if (searchDto.getKey().equals("buildingName")) {
            SearchByName search = new SearchByName(page, searchDto.getSize(), searchDto.getValue(), "");
            absentList = absentDao.queryByName(search);
            count = absentDao.queryByNameCount(search);
        } else if (searchDto.getKey().equals("dormitoryName")) {
            SearchByName search = new SearchByName(page, searchDto.getSize(), "", searchDto.getValue());
            absentList = absentDao.queryByName(search);
            count = absentDao.queryByNameCount(search);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("absentList", absentList);
        map.put("count", count);
        return map;
    }
}
